<?php
include "counter2.php";

 requirelogin();
 $u = User::create(); // object for currently logged-in user
 pagestart();

print "<HTML>\n";
pagehead(_("Home page for #") . $u->userid . " ".  $u->pfields[name]);
print "\n<BODY>\n";
pagetop();

iamashowpage();
showmessage();

// if ($nocookie) {
// print "<strong>" . _("Note") . ":</strong> " . _("You seem to be running without cookies enabled.") . "<br>\n" .
// _("This may cause some problems; support for running without cookies is <em>very</em> new and untested.") .
// "<br>\n" . _("The login screen may pop up again at unexpected moments, and some functions may simply refuse to work.") .
// "<br>\n" .
// _("Please report problems to") . emailCloaking("help@counter.li.org",0) .
// ".\n<p>\n";
// }

print "<h1><img src=\"/gifs/Logo-small.png\" alt=\"" . _("Linux Counter Logo") . "\">";
print _("Home page for #") . $u->userid . " " .  $u->pfields[name]; 
print "</h1>\n";
print "<h2>" . _("My registration information") . "</h2>\n";
print "<table class=\"info\">\n" .
"<tr>\n<td>" . _("Registration created") . "</td>\n" .
"<td>" . $u->ufields[f_ctime] . "</td>\n</tr>\n" .
"<tr>\n<td>" . _("Name") . "</td>\n" .
"<td>" . $u->pfields[name] . "</td>\n</tr>\n" .
"<tr>\n<td>" . _("Email (private)") . "</td>\n" .
"<td>" . emailCloaking($u->ufields[email],0) . "</td>\n</tr>\n" .
"<tr>\n<td>" . _("Email (public)") . "</td>\n<td>";
$email_addresses = $u->get_public_email_addresses();
$count = count($email_addresses);
$index = 0;
while ($index < $count) {
   print emailCloaking($email_addresses[$index],0) . "<br>\n";
   ++ $index;
}
print "</td>\n</tr>\n<tr>\n<td>" . _("Homepage") . "</td>\n";
if ($u->pfields[homepage] != "") {
   if (!preg_match("/^http:\/\//i", $u->pfields[homepage]) && !preg_match("/^https:\/\//i", $u->pfields[homepage])) {
      print "<td><a href=\"http://" . $u->pfields[homepage] .
	"\" target=\"_blank\">http://" . $u->pfields[homepage] . "</a></td>";
   } else {
      print "<td><a href=\"" . $u->pfields[homepage] . "\" target=\"_blank\">" . $u->pfields[homepage] . "</a></td>";
   } 
}
print "\n</tr>\n" . 
"<tr>\n<td>" . _("Country") . "</td>\n" .
"<td>" . $u->pfields[country] . "</td>\n</tr>\n" .
"<tr>\n<td>" . _("State/province/department") . "</td>\n" .
"<td>" . $u->pfields[state] . "</td>\n</tr>\n" .
"<tr>\n<td>" . _("City") . "</td>\n" .
"<td>" . $u->pfields[city] . "</td>\n</tr>\n" .
"</table>\n" .
"<p>\n";
print _("You live in") . " ";
    $placeid = mysql_escape_string($u->pcfields[placeid]);
    $placeq = mysql_query("select * from places where name = '$placeid'");
    if ($placeq) {
       $place = mysql_fetch_array($placeq);
       if ($place) {
          # echo "Generating place bar for $place[longname]";
          place_bar($place);
       } else {
          echo "Did not find any entry for $placeid";
       }
    } else {
       echo "Error in fetching $placeid";
    }
print "\n<p>\n" .
"<a href=\"/cgi-bin/runscript/show-user.cgi" . maybecookie() . "\" class=\"button\">" .
_("Edit my person entry") .
"</a>\n" .
"<p>\n" .
"<a href=\"emails.php\" class=\"button\">" . _("Edit my emails") . "</a>\n" .
"<p>\n" .
"<a href=\"/cgi-bin/runscript/logout.cgi\" class=\"button\">" . _("Log Out") . "</a>\n";

print "<h2>" . _("My machines registered") . "</h2>\n";

   $query = mysql_query("SELECT machines.f_key, machines.name, unix_timestamp(automach.updated) as updatetime, automach.uptime FROM machines LEFT JOIN automach ON machines.f_key = automach.f_key WHERE machines.owner = '" . currentuser() . "' ORDER BY f_key");
   if (!$query) {
         print mysql_error();
         die("Query failed");
   }

print "<table class=\"info\">\n" .
"<tr><th>" . _("ID") . "<th>" . _("Name") . "<th>" . _("Last auto-update") . "<th>" . _("Uptime") . "</tr>\n";

   while ($arr = mysql_fetch_array($query)) {
	# Try to suppress irritation from automach duplicates
	if ($prevkey <> $arr[f_key]) {
	    print "<tr><td>" . $arr[f_key];
            print "<td><a href=\"/cgi-bin/runscript/machine-show.cgi?machine=$arr[f_key]";
	    maybecookie("&amp;");
	    print "\">";
	    if ($arr[name]) {
	       print $arr[name];
            } else {
	       print _("Nameless");
            }
	    print "</a>";
	    $prevkey = $arr[f_key];
        } else {
	   print "<tr><td> - " . _("also reported at") . " - <td>";
       }
	print "<td>";
	if ($arr[updatetime]) {
	    print strftime("%Y-%m-%d %H:%M:%S", $arr[updatetime]);
   	} else {
	    print "<a href=\"/scripts/\">" . _("No automatic update") . "</a>";
        }
	if ($arr[uptime]) {
	    printf("<td>%.1f", $arr[uptime]);
        }
	print "\n";
   }

print "</table>\n<br>\n" .
"<a href=\"/machine-form.php\" class=\"button\">" . _("Add a machine") . "</a>\n" .
"<p>\n" .
_("Grab the ") . "<a href=\"/scripts/\" class=\"button\">" . _("machine update script") . "</a>\n" .
"<p>\n" .
_("All information stored in the Linux Counter is subject to the Counter") . " <a href=\"/organization/privacy.php\">" . _("privacy policy") . "</a>.\n";

pagebottom("yes");
print "</body>\n" .
"</html>";
?>
